﻿using UnityEngine;
using System.Collections;
using System;

public class UIKeyBorad : UI_Base
{
    public UILabel mNumberLb;
    public Transform mContainer;
    public string mNumberStr;
    public int mLen = 9;
    private UIKeyBoradVI mVI;


    public event Action<string> OnClickKey;
    public event Action<string> OnClickKeyBack;
    public event Action<string> OnConfirmKey;


    public override bool Init(object arg)
    {
        mVI = arg as UIKeyBoradVI;
        if (mVI != null)
        {
            mNumberStr = mVI.Number;
        }
        return true;
    }

    public void SetNumber(string str)
    {
        mNumberStr = str;
        if (mNumberLb != null)
            mNumberLb.text = str;
    }


    public void OnClickKeyBoard(GameObject go)
    {
        string key = go.name;
        if (OnClickKey != null)
            OnClickKey(key);


        //处理信息
        string str = mNumberStr;
        if (string.IsNullOrEmpty(str))
        {
            str = string.Empty;
        }

        switch (key)
        {
            case "0":
                if (string.IsNullOrEmpty(str) == false && str != "0")
                {
                    str += key;
                }
                else if (string.IsNullOrEmpty(str) && key == "0")
                    str = key;
                break;
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                if (str == "0")
                    str = key;
                else
                    str = str + key;
                break;
            case "10":          //退格
                if (string.IsNullOrEmpty(str) == false)
                    str = str.Remove(str.Length - 1, 1);
                break;
        }

        if (str.Length >= mLen)
            return;

        if (mNumberLb != null)
            mNumberLb.text = str;

        mNumberStr = str;


        if (OnClickKeyBack != null)
            OnClickKeyBack(mNumberStr);

        if (key == "11")
        {
            if (OnConfirmKey != null)
                OnConfirmKey(str);

            OnClickClose();
        }


    }

    public void OnCancel()
    {

        if (OnConfirmKey != null)
            OnConfirmKey(mNumberStr);

        OnClickClose();
    }


}

public class UIKeyBoradVI
{
    public string Number { get; set; }
}